/**   
 * @Title: WMainActivity.java
 * @Package com.willter.weather.app
 * @Description: TODO(描述文件)
 * 		<p>
 * @author willter
 * @date 2012-9-17
 */
package com.willter.weather.app;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.willter.weather.utils.WebAccessTools;

/**
 * @ClassName: WMainActivity
 * @Description: 天气预报主界面
 *               <p>
 * @author willter
 * @date 2012-9-17
 */
public class MainActivity extends Activity {
	// 记录壁纸的文件
	public static final String WALLPAPER_FILE = "wallpaper_file";
	// 缓存天气的文件
	public static final String STORE_WEATHER = "store_weather";
	// 菜单加载器
	private MenuInflater mi;
	
	private ViewPager viewPager;
	
	private View jinView,mingView,houView;
	// 根布局
	private LinearLayout jinRootLayout,mingRootLayout,houRootLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);

		// 初始化ViewPager的视图集合
		viewPager = (ViewPager)this.findViewById(R.id.viewPager);
		LayoutInflater mLi = LayoutInflater.from(this);
		jinView = mLi.inflate(R.layout.jinmain, null);
		mingView = mLi.inflate(R.layout.mingmain, null);
		houView = mLi.inflate(R.layout.houmain, null);
		
		final List<View> views = new ArrayList<View>();
		views.add(jinView);
		views.add(mingView);
		views.add(houView);
		
		// 填充ViewPager的数据适配器
		PagerAdapter pagerAdapter = new PagerAdapter() {
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return views.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager) container).removeView(views.get(position));
			}

			@Override
			public CharSequence getPageTitle(int position) {
				return null;
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(views.get(position));
				return views.get(position);
			}
		};
		viewPager.setAdapter(pagerAdapter);
		
		jinRootLayout = (LinearLayout)jinView.findViewById(R.id.rootLayout);
		mingRootLayout = (LinearLayout)mingView.findViewById(R.id.rootLayout);
		houRootLayout = (LinearLayout)houView.findViewById(R.id.rootLayout);
		//rootLayout = (LinearLayout) findViewById(R.id.rootLayout);
		// 实例化菜单
		mi = new MenuInflater(this);

		// 检查程序中的缓存文件判断是否第一次运行
		String dirPath = "/data/data/com.willter.weather.app/shared_prefs/";
		File file = new File(dirPath);
		boolean isFirstRun = false;
		if (!file.exists()) {
			Editor editor = getSharedPreferences(WALLPAPER_FILE, MODE_PRIVATE).edit();
			editor.putInt("wellpaper", R.drawable.app_bg02);
			editor.commit();

			isFirstRun = true;
		} else {
			SharedPreferences sp = getSharedPreferences(SetCityActivity.CITY_CODE_FILE, MODE_PRIVATE);
			jinRootLayout.setBackgroundResource(sp.getInt("wellpaper", R.drawable.app_bg02));
			mingRootLayout.setBackgroundResource(sp.getInt("wellpaper", R.drawable.app_bg02));
			houRootLayout.setBackgroundResource(sp.getInt("wellpaper", R.drawable.app_bg02));
		}

		// 得到保存的城市天气
		SharedPreferences sp = getSharedPreferences(SetCityActivity.CITY_CODE_FILE, MODE_PRIVATE);
		String cityCode = sp.getString("code", "");
		if (cityCode != null && cityCode.trim().length() != 0) {
			SharedPreferences shared = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE);
			long currentTime = System.currentTimeMillis();
			// 得到天气缓冲文件中的有效期
			long vaildTime = shared.getLong("validTime", currentTime);
			// 比较天气缓存文件中的有效期，如果超时了，则访问网络更新天气
			if (vaildTime > currentTime) {
				setWeatherSituation(shared);
			} else {
				setWeatherSituation(cityCode);
			}
		} else {
			// 跳转到设置城市的Activity
			Intent intent = new Intent(MainActivity.this, SetCityActivity.class);
			intent.putExtra("isFirstRun", isFirstRun);
			startActivityForResult(intent, 0);
		}
	}

	/**
	 * @Title: onActivityResult
	 * @Description: 设置城市界面的回退
	 *               <p>
	 * @author willter
	 * @date 2012-9-17
	 *       <p>
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 * @see android.app.Activity#onActivityResult(int, int,
	 *      android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		SharedPreferences sp = getSharedPreferences(SetCityActivity.CITY_CODE_FILE, MODE_PRIVATE);
		String cityCode = sp.getString("code", "");
		if (cityCode != null && cityCode.trim().length() != 0) {
			if (data != null && data.getBooleanExtra("updateWeather", false)) {
				// 从网上更新新的天气
				setWeatherSituation(cityCode);
			} else {
				// 读取缓存文件中的天气
				SharedPreferences shared = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE);
				setWeatherSituation(shared);
			}
		} else {
			// 如果是没有城市码的回退，则退出程序
			MainActivity.this.finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 加载菜单资源，实例化菜单
		mi.inflate(R.menu.configure_menu, menu);
		// 得到保存的壁纸
		SharedPreferences sp = getSharedPreferences(WALLPAPER_FILE, MODE_PRIVATE);
		SubMenu subMenu = menu.getItem(2).getSubMenu();
		MenuItem item = null;
		switch (sp.getInt("wellpaper", R.drawable.app_bg02)) {
			case R.drawable.app_bg01:
				item = subMenu.getItem(0);
				item.setChecked(true);
				break;
			case R.drawable.app_bg02:
				item = subMenu.getItem(1);
				item.setChecked(true);
				break;
			case R.drawable.app_bg03:
				item = subMenu.getItem(2);
				item.setChecked(true);
				break;
			case R.drawable.app_bg04:
				item = subMenu.getItem(3);
				item.setChecked(true);
				break;
			default:
				break;
		}

		return true;
	}

	/**
	 * @Title: onOptionsItemSelected
	 * @Description: 单击菜单调用方法
	 *               <p>
	 * @author willter
	 * @date 2012-9-17
	 *       <p>
	 * @param item
	 * @return
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// 得到SharedPreferences操作对象更改壁纸
		Editor editor = getSharedPreferences(WALLPAPER_FILE, MODE_PRIVATE).edit();
		// 得到城市的编码
		SharedPreferences sp = getSharedPreferences(SetCityActivity.CITY_CODE_FILE, MODE_PRIVATE);
		switch (item.getItemId()) {
			case R.id.menu_changeCity:
				// 跳转到设置城市的Activity
				Intent intent = new Intent(MainActivity.this, SetCityActivity.class);
				startActivityForResult(intent, 0);
				break;
			case R.id.menu_update:
				String cityCode = sp.getString("code", "");
				if (cityCode != null && cityCode.trim().length() != 0) {
					setWeatherSituation(cityCode);
				}
				// 更换壁纸
			case R.id.wallpaper01:
				jinRootLayout.setBackgroundResource(R.drawable.app_bg01);
				mingRootLayout.setBackgroundResource(R.drawable.app_bg01);
				houRootLayout.setBackgroundResource(R.drawable.app_bg01);
				editor.putInt("wellpaper", R.drawable.app_bg01);
				editor.commit();
				item.setChecked(true);
				break;
			case R.id.wallpaper02:
				jinRootLayout.setBackgroundResource(R.drawable.app_bg02);
				mingRootLayout.setBackgroundResource(R.drawable.app_bg02);
				houRootLayout.setBackgroundResource(R.drawable.app_bg02);
				editor.putInt("wellpaper", R.drawable.app_bg02);
				editor.commit();
				item.setChecked(true);
				break;
			case R.id.wallpaper03:
				jinRootLayout.setBackgroundResource(R.drawable.app_bg03);
				mingRootLayout.setBackgroundResource(R.drawable.app_bg03);
				houRootLayout.setBackgroundResource(R.drawable.app_bg03);
				editor.putInt("wellpaper", R.drawable.app_bg03);
				editor.commit();
				item.setChecked(true);
				break;
			case R.id.wallpaper04:
				jinRootLayout.setBackgroundResource(R.drawable.app_bg04);
				mingRootLayout.setBackgroundResource(R.drawable.app_bg04);
				houRootLayout.setBackgroundResource(R.drawable.app_bg04);
				editor.putInt("wellpaper", R.drawable.app_bg04);
				editor.commit();
				item.setChecked(true);
				break;
			default:
				break;
		}

		return true;
	}

	public void setWeatherSituation(String cityCode) {
		String info = "http://m.weather.com.cn/data/" + cityCode + ".html";
		info = new WebAccessTools(this).getWebContent(info);

		// ==========================解析JSON得到天气===========================
		try {
			JSONObject json = new JSONObject(info).getJSONObject("weatherinfo");
			TextView tempText = null;
			ImageView imageView = null;
			int weather_icon = 0;

			// 建立一个缓存天气的文件
			Editor editor = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE).edit();

			/************************************页卡1**************************************/
			// 得到城市
			info = json.getString("city");
			tempText = (TextView) jinView.findViewById(R.id.cityField);
			tempText.setText(info);
			editor.putString("city", info);

			// 得到阳历日期
			info = json.getString("date_y");
			info = info + "(" + json.getString("week") + ")";
			tempText = (TextView) jinView.findViewById(R.id.date_y);
			tempText.setText(info);
			editor.putString("date_y", info);
			// 得到农历
			info = json.getString("date");
			tempText = (TextView) jinView.findViewById(R.id.date);
			tempText.setText(info);
			editor.putString("date", info);
			// 得到温度
			info = json.getString("temp1");
			tempText = (TextView) jinView.findViewById(R.id.currentTemp);
			tempText.setText(info);
			editor.putString("temp1", info);
			// 得到天气
			info = json.getString("weather1");
			tempText = (TextView) jinView.findViewById(R.id.currentWeather);
			tempText.setText(info);
			editor.putString("weather1", info);
			// 天气图标
			info = json.getString("img_title1");
			imageView = (ImageView) jinView.findViewById(R.id.weather_icon01);
			weather_icon = getWeatherBitMapResource(info);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title1", weather_icon);
			// 得到风向
			info = json.getString("wind1");
			tempText = (TextView) jinView.findViewById(R.id.currentWind);
			tempText.setText(info);
			editor.putString("wind1", info);
			// 得到建议
			info = json.getString("index_d");
			tempText = (TextView) jinView.findViewById(R.id.index_d);
			tempText.setText(info);
			editor.putString("index_d", info);

			// 得到明天的天气
			info = json.getString("weather2");
			tempText = (TextView) jinView.findViewById(R.id.weather02);
			tempText.setText(info);
			editor.putString("weather2", info);
			// 明天的图标
			info = json.getString("img_title2");
			imageView = (ImageView) jinView.findViewById(R.id.weather_icon02);
			weather_icon = getWeatherBitMapResource(info);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title2", weather_icon);
			// 明天的气温
			info = json.getString("temp2");
			tempText = (TextView) jinView.findViewById(R.id.temp02);
			tempText.setText(info);
			editor.putString("temp2", info);
			// 明天的风力
			info = json.getString("wind2");
			tempText = (TextView) jinView.findViewById(R.id.wind02);
			tempText.setText(info);
			editor.putString("wind2", info);

			// 后天的天气
			info = json.getString("weather3");
			tempText = (TextView) jinView.findViewById(R.id.weather03);
			tempText.setText(info);
			editor.putString("weather3", info);
			// 后天天气图标
			info = json.getString("img_title3");
			imageView = (ImageView) jinView.findViewById(R.id.weather_icon03);
			weather_icon = getWeatherBitMapResource(info);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title3", weather_icon);
			// 后天的气温
			info = json.getString("temp3");
			tempText = (TextView) jinView.findViewById(R.id.temp03);
			tempText.setText(info);
			editor.putString("temp3", info);
			// 后天的风力
			info = json.getString("wind3");
			tempText = (TextView) jinView.findViewById(R.id.wind03);
			tempText.setText(info);
			editor.putString("wind3", info);
			/*******************************************************************************/
			
			/************************************页卡2**************************************/
			// 得到城市
			info = json.getString("city");
			tempText = (TextView) mingView.findViewById(R.id.cityField);
			tempText.setText(info);
			editor.putString("city", info);

			// 得到阳历日期
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, 1);
			int week = c.get(Calendar.DAY_OF_WEEK);
			Date date2 = c.getTime();
			DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			String dateStr = format.format(date2);
			info = dateStr + "(" + getWeek(week) + ")";
//			info = json.getString("date_y");
//			info = info + "(" + json.getString("week") + ")";
			tempText = (TextView) mingView.findViewById(R.id.date_y);
			tempText.setText(info);
			editor.putString("date_y_24", info);
			// 得到农历
			info = json.getString("date");
			tempText = (TextView) mingView.findViewById(R.id.date);
			tempText.setText(info);
			editor.putString("date", info);
			// 得到温度
			info = json.getString("temp2");
			tempText = (TextView) mingView.findViewById(R.id.currentTemp);
			tempText.setText(info);
			editor.putString("temp2", info);
			// 得到天气
			info = json.getString("weather2");
			tempText = (TextView) mingView.findViewById(R.id.currentWeather);
			tempText.setText(info);
			editor.putString("weather2", info);
			// 天气图标
			info = json.getString("img_title2");
			imageView = (ImageView) mingView.findViewById(R.id.weather_icon01);
			weather_icon = getWeatherBitMapResource(info);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title2", weather_icon);
			// 得到风向
			info = json.getString("wind2");
			tempText = (TextView) mingView.findViewById(R.id.currentWind);
			tempText.setText(info);
			editor.putString("wind2", info);
			// 得到建议
			info = json.getString("index48_d");
			tempText = (TextView) mingView.findViewById(R.id.index_d);
			tempText.setText(info);
			editor.putString("index48_d", info);
			/*******************************************************************************/
			
			/************************************页卡3**************************************/
			// 得到城市
			info = json.getString("city");
			tempText = (TextView) houView.findViewById(R.id.cityField);
			tempText.setText(info);
			editor.putString("city", info);

			// 得到阳历日期
			c = Calendar.getInstance();
			c.add(Calendar.DATE, 2);
			week = c.get(Calendar.DAY_OF_WEEK);
			date2 = c.getTime();
			dateStr = format.format(date2);
			info = dateStr + "(" + getWeek(week) + ")";
//			info = json.getString("date_y");
//			info = info + "(" + json.getString("week") + ")";
			tempText = (TextView) houView.findViewById(R.id.date_y);
			tempText.setText(info);
			editor.putString("date_y_48", info);
			// 得到农历
			info = json.getString("date");
			tempText = (TextView) houView.findViewById(R.id.date);
			tempText.setText(info);
			editor.putString("date", info);
			// 得到温度
			info = json.getString("temp3");
			tempText = (TextView) houView.findViewById(R.id.currentTemp);
			tempText.setText(info);
			editor.putString("temp3", info);
			// 得到天气
			info = json.getString("weather3");
			tempText = (TextView) houView.findViewById(R.id.currentWeather);
			tempText.setText(info);
			editor.putString("weather3", info);
			// 天气图标
			info = json.getString("img_title3");
			imageView = (ImageView) houView.findViewById(R.id.weather_icon01);
			weather_icon = getWeatherBitMapResource(info);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title3", weather_icon);
			// 得到风向
			info = json.getString("wind3");
			tempText = (TextView) houView.findViewById(R.id.currentWind);
			tempText.setText(info);
			editor.putString("wind3", info);
			// 得到建议
			info = json.getString("index48_d");
			tempText = (TextView) houView.findViewById(R.id.index_d);
			tempText.setText(info);
			editor.putString("index48_d", info);
			/*******************************************************************************/
			
			// 设置一个有效日期为5小时
			long validTime = System.currentTimeMillis();
			validTime = validTime + 5 * 60 * 60 * 1000;
			editor.putLong("validTime", validTime);

			// 保存
			editor.commit();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Title: setWeatherSituation
	 * @Description: 根据已定的缓存文件来得到天气情况
	 *               <p>
	 * @author willter
	 * @date 2012-9-17
	 *       <p>
	 * @param shared
	 */
	public void setWeatherSituation(SharedPreferences shared) {
		String info = null;
		TextView tempText = null;
		ImageView imageView = null;

		// 得到城市
		info = shared.getString("city", "");
		tempText = (TextView) jinView.findViewById(R.id.cityField);
		tempText.setText(info);

		// 得到阳历日期
		info = shared.getString("date_y", "");
		tempText = (TextView) jinView.findViewById(R.id.date_y);
		tempText.setText(info);
		// 得到农历
		info = shared.getString("date", "");
		tempText = (TextView) jinView.findViewById(R.id.date);
		tempText.setText(info);
		// 得到温度
		info = shared.getString("temp1", "");
		tempText = (TextView) jinView.findViewById(R.id.currentTemp);
		tempText.setText(info);
		// 得到天气
		info = shared.getString("weather1", "");
		tempText = (TextView) jinView.findViewById(R.id.currentWeather);
		tempText.setText(info);
		// 天气图标
		imageView = (ImageView) jinView.findViewById(R.id.weather_icon01);
		imageView.setImageResource(shared.getInt("img_title1", 0));
		// 得到风向
		info = shared.getString("wind1", "");
		tempText = (TextView) jinView.findViewById(R.id.currentWind);
		tempText.setText(info);
		// 得到建议
		info = shared.getString("index_d", "");
		tempText = (TextView) jinView.findViewById(R.id.index_d);
		tempText.setText(info);

		// 得到明天的天气
		info = shared.getString("weather2", "");
		tempText = (TextView) jinView.findViewById(R.id.weather02);
		tempText.setText(info);
		// 明天的图标
		imageView = (ImageView) jinView.findViewById(R.id.weather_icon02);
		imageView.setImageResource(shared.getInt("img_title2", 0));
		// 明天的气温
		info = shared.getString("temp2", "");
		tempText = (TextView) jinView.findViewById(R.id.temp02);
		tempText.setText(info);
		// 明天的风力
		info = shared.getString("wind2", "");
		tempText = (TextView) jinView.findViewById(R.id.wind02);

		// 后天的天气
		info = shared.getString("weather3", "");
		tempText = (TextView) jinView.findViewById(R.id.weather03);
		tempText.setText(info);
		// 后天天气图标
		imageView = (ImageView) jinView.findViewById(R.id.weather_icon03);
		imageView.setImageResource(shared.getInt("img_title3", 0));
		// 后天的气温
		info = shared.getString("temp3", "");
		tempText = (TextView) jinView.findViewById(R.id.temp03);
		tempText.setText(info);
		// 后天的风力
		info = shared.getString("wind3", "");
		tempText = (TextView) jinView.findViewById(R.id.wind03);
		tempText.setText(info);
	}

	// 由天气情况得到图片
	public static int getWeatherBitMapResource(String weather) {
		Log.i("weather_info", "=============" + weather + "===============");
		if (weather.equals("晴")) {
			return R.drawable.weathericon_condition_01;
		} else if (weather.equals("多云")) {
			return R.drawable.weathericon_condition_02;
		} else if (weather.equals("阴")) {
			return R.drawable.weathericon_condition_04;
		} else if (weather.equals("雾")) {
			return R.drawable.weathericon_condition_05;
		} else if (weather.equals("沙尘暴")) {
			return R.drawable.weathericon_condition_06;
		} else if (weather.equals("阵雨")) {
			return R.drawable.weathericon_condition_07;
		} else if (weather.equals("小雨") || weather.equals("小到中雨")) {
			return R.drawable.weathericon_condition_08;
		} else if (weather.equals("大雨")) {
			return R.drawable.weathericon_condition_09;
		} else if (weather.equals("雷阵雨")) {
			return R.drawable.weathericon_condition_10;
		} else if (weather.equals("小雪")) {
			return R.drawable.weathericon_condition_11;
		} else if (weather.equals("大雪")) {
			return R.drawable.weathericon_condition_12;
		} else if (weather.equals("雨夹雪")) {
			return R.drawable.weathericon_condition_13;
		} else {
			return R.drawable.weathericon_condition_17;
		}
	}
	
	public static String getWeek(Integer week) {
		String weekStr = "";
		switch (week) {
			case 1:
				weekStr = "星期日";
				break;
			case 2:
				weekStr = "星期一";
				break;
			case 3:
				weekStr = "星期二";
				break;
			case 4:
				weekStr = "星期三";
				break;
			case 5:
				weekStr = "星期四";
				break;
			case 6:
				weekStr = "星期五";
				break;
			case 7:
				weekStr = "星期六";
				break;
			default:
				break;
		}
		return weekStr;
	}
}
